package com.techventus.ors.craigslist;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;

import com.zenkey.net.prowser.Prowser;
import com.zenkey.net.prowser.Request;
import com.zenkey.net.prowser.Response;
import com.zenkey.net.prowser.Tab;

//This class very simply crawls the RSS Feeds
//on the rideshare listings in each region.
public class Crawler {
	static Prowser p = new Prowser();
	
	static int exCount = 0;
	
	static String USER_AGENT = 	"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4";

	public static void main(String[] args){
		
		if(args.length<2)
		{
			System.out.println("First argument: {raw data save path}");
			System.out.println("Second argument: {log save path}");
			return;
		}
//		p.set
		
		p.setDefaultUserAgent(USER_AGENT);
		Tab tab = p.createTab();
		try{
			gatherYeCraigslists(USER_AGENT, tab, args[0], args[1]);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	

	public static String fetch(String url_base, String id, String user_agent, Tab tab)
			throws URISyntaxException {
		Request req = new Request(url_base+id);
		req.setUserAgent(user_agent);
		
		Response resp = tab.go(req);
		String html = resp.getPageSource();
		return html;
	}
	
	
	
	
	

	private static void writeLog(String yourstring, String logLocation){
		BufferedWriter writer = null;
		try
		{
			System.out.println("Printing "+yourstring.length());
			
			writer = new BufferedWriter( new FileWriter( logLocation, true));
			
			writer.write( yourstring+"\n\r");
			
		}
		catch ( IOException e)
		{
		}
		finally
		{
			try
			{
				if ( writer != null)
					writer.close( );
			}
			catch ( IOException e)
			{
			}
	     }
	}
	
	private static void write(String yourstring, String saveLocation){
		BufferedWriter writer = null;
		try
		{
			System.out.println("Printing "+yourstring.length());
			
			writer = new BufferedWriter( new FileWriter( saveLocation, true));
			
			writer.write( yourstring);
			writer.newLine();
			writer.write("*****************************");
			writer.newLine();
		}
		catch ( IOException e)
		{
		}
		finally
		{
			try
			{
				if ( writer != null)
					writer.close( );
			}
			catch ( IOException e)
			{
			}
	     }
	}
	
	
	public static void gatherYeCraigslists(String user_agent, Tab tab , String saveLocation, String logLocation)
	{
		int i =0;
		for(Region region:Region.values()){
			if(exCount >= 20)
				break;
			try
			{
				Thread.sleep(3000);
				System.out.println();
				String htmlResult = fetch(region.getUrl()+"/rid/index.rss", "", user_agent, tab);
				write(htmlResult, saveLocation);
				writeLog("Success "+i+" "+region.getCity(), logLocation);
			}catch(Exception e){
				writeLog("Error "+i, logLocation);
				exCount++;
			}
			i++;
		}
	}
	

}
